﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClientWPF.localhost;

namespace ClientWPF
{
    public partial class AddUserForm : Window
    {
        Service1 service = new Service1();
        AuthentificationCredentials authentification = new AuthentificationCredentials();

        public AddUserForm()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void AddUserButton_Click(object sender, RoutedEventArgs e)
        {
            authentification.Privileges = "admin";
            service.AuthentificationCredentialsValue = authentification;

            User user = new User();
            user.Name = NameBox.Text;

            if (!service.CheckUserExist(user))
            {
                if (PasswordBox.Password.Equals(RepeatPasswordBox.Password))
                {
                    if (!(PasswordBox.Password.Equals("") || NameBox.Text.Equals("")))
                    {
                        if (!((PasswordBox.Password.Length > 30) || (NameBox.Text.Length > 30)))
                        {
                            if (!(PrivilegesComboBox.Text.Equals("")))
                            {
                                user.Password = PasswordBox.Password;
                                user.Privileges = PrivilegesComboBox.Text;
                                service.AddUser(user);
                                MessageBox.Show("User " + user.Name + " added successfully!");
                                Close();
                            }
                            else MessageBox.Show("You must choose privileges!");
                        }
                        else MessageBox.Show("The length of name and password cannot be longer than 30 characters!");
                    }
                    else MessageBox.Show("One or more fields are blanc!");
                }
                else MessageBox.Show("Passwords do not match!");
            }
            else MessageBox.Show("User " + user.Name + " already exists!");
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
